package com.example.drink_order_system.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

import com.example.drink_order_system.R;
import com.example.drink_order_system.entity.discount_quan;

import java.util.ArrayList;
import java.util.Date;

import DataBase.UserDbHelper;

public class shop_page extends AppCompatActivity {
    private GridView shop_gridView;
    private ImageButton back;
    private static int count;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shop_page_layout);
        shop_gridView = findViewById(R.id.shop_gridView);
        back = findViewById(R.id.score_shop_page_back);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              finish();
            }
        });

        my_grid_adapter my_grid_adapter = new my_grid_adapter();
        shop_gridView.setAdapter(my_grid_adapter);

    }




    class my_grid_adapter extends BaseAdapter{
        private ArrayList<discount_quan> dis;
        private TextView cost;
        private TextView detail;
        private AppCompatButton exchange;
        public my_grid_adapter() {
            super();
            dis = discount_quan.getShow_discount();
        }

        @Override
        public int getCount() {
            return dis.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = View.inflate(getApplicationContext(),R.layout.shop_item_layout,null);
            detail = view.findViewById(R.id.shop_exchange_detail);
            cost = view.findViewById(R.id.exchange_cost);
            exchange = view.findViewById(R.id.shop_exchange_btn);
            detail.setText(dis.get(i).getDetail());
            cost.setText("所需积分："+dis.get(i).getScore());
            exchange.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Date date = new Date();
                    String now_date = (date.getYear()+1900)+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日";
                    String end_date = (date.getYear()+1900)+"年"+(date.getMonth()+1)+"月"+(date.getDate()+5)+"日";
                    UserDbHelper db = (UserDbHelper) UserDbHelper.getSqliteHelper(getApplicationContext());
                    int final_score = MainActivity.now_user.getScore();
                    switch (i){
                        case 0 :
                            if (MainActivity.now_user.getScore()<500){
                                Toast.makeText(shop_page.this, "积分不足！", Toast.LENGTH_SHORT).show();
                            }else {
                                discount_quan dis1 = new discount_quan("1元优惠券",500,now_date,end_date,count++);
                                discount_quan.add_discount_quan(dis1);
                                db.saveDiscount(MainActivity.now_user.getUsername(),dis1);
                                db.save_dis_num(MainActivity.now_user.getUsername(),discount_quan.getDiscountQuans().size());
                                final_score -= 500;
                                MainActivity.now_user.setScore(final_score);
                                Toast.makeText(shop_page.this, "兑换成功！", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case 1 :
                            if (MainActivity.now_user.getScore()<1000){
                                Toast.makeText(shop_page.this, "积分不足！", Toast.LENGTH_SHORT).show();
                            }else {
                                discount_quan dis2 = new discount_quan("2元优惠券",1000,now_date,end_date,count++);
                                discount_quan.add_discount_quan(dis2);
                                db.saveDiscount(MainActivity.now_user.getUsername(),dis2);
                                db.save_dis_num(MainActivity.now_user.getUsername(),discount_quan.getDiscountQuans().size());
                                final_score -= 1000;
                                MainActivity.now_user.setScore(final_score);
                                Toast.makeText(shop_page.this, "兑换成功！", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case 2 :
                            if (MainActivity.now_user.getScore()<1500){
                                Toast.makeText(shop_page.this, "积分不足！", Toast.LENGTH_SHORT).show();
                            }else {
                                discount_quan dis3 = new discount_quan("2.5元优惠券",1500,now_date,end_date,count++);
                                discount_quan.add_discount_quan(dis3);
                                db.saveDiscount(MainActivity.now_user.getUsername(),dis3);
                                db.save_dis_num(MainActivity.now_user.getUsername(),discount_quan.getDiscountQuans().size());
                                final_score -= 1500;
                                MainActivity.now_user.setScore(final_score);
                                Toast.makeText(shop_page.this, "兑换成功！", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case 3 :
                            if (MainActivity.now_user.getScore()<2000){
                                Toast.makeText(shop_page.this, "积分不足！", Toast.LENGTH_SHORT).show();
                            }else {
                                discount_quan dis4 = new discount_quan("3元优惠券",2000,now_date,end_date,count++);
                                discount_quan.add_discount_quan(dis4);
                                db.saveDiscount(MainActivity.now_user.getUsername(),dis4);
                                db.save_dis_num(MainActivity.now_user.getUsername(),discount_quan.getDiscountQuans().size());
                                final_score -= 2000;
                                MainActivity.now_user.setScore(final_score);
                                Toast.makeText(shop_page.this, "兑换成功！", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case 4 :
                            if (MainActivity.now_user.getScore()<2500){
                                Toast.makeText(shop_page.this, "积分不足！", Toast.LENGTH_SHORT).show();
                            }else {
                                discount_quan dis5 = new discount_quan("4元优惠券",2500,now_date,end_date,count++);
                                discount_quan.add_discount_quan(dis5);
                                db.saveDiscount(MainActivity.now_user.getUsername(),dis5);
                                db.save_dis_num(MainActivity.now_user.getUsername(),discount_quan.getDiscountQuans().size());
                                final_score -= 2500;
                                MainActivity.now_user.setScore(final_score);
                                Toast.makeText(shop_page.this, "兑换成功！", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case 5 :
                            if (MainActivity.now_user.getScore()<3000){
                                Toast.makeText(shop_page.this, "积分不足！", Toast.LENGTH_SHORT).show();
                            }else {
                                discount_quan dis6 = new discount_quan("5元优惠券",3000,now_date,end_date,count++);
                                discount_quan.add_discount_quan(dis6);
                                db.saveDiscount(MainActivity.now_user.getUsername(),dis6);
                                db.save_dis_num(MainActivity.now_user.getUsername(),discount_quan.getDiscountQuans().size());
                                final_score -= 3000;
                                MainActivity.now_user.setScore(final_score);
                                Toast.makeText(shop_page.this, "兑换成功！", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case 6 :
                            if (MainActivity.now_user.getScore()<5000){
                                Toast.makeText(shop_page.this, "积分不足！", Toast.LENGTH_SHORT).show();
                            }else {
                                discount_quan dis7 = new discount_quan("免费一次（仅限20元以下）",5000,now_date,end_date,count++);
                                discount_quan.add_discount_quan(dis7);
                                db.saveDiscount(MainActivity.now_user.getUsername(),dis7);
                                db.save_dis_num(MainActivity.now_user.getUsername(),discount_quan.getDiscountQuans().size());
                                final_score -= 5000;
                                MainActivity.now_user.setScore(final_score);
                                Toast.makeText(shop_page.this, "兑换成功！", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        default:
                            break;
                    }
                    db.saveScore(MainActivity.now_user.getUsername(),final_score);

                }
            });
            return view;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        SharedPreferences sp = getSharedPreferences("count_num",MODE_PRIVATE);
        count = sp.getInt("count",0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SharedPreferences sp = getSharedPreferences("count_num",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt("count",count);
        editor.commit();
    }
}
